package cn.tedu.boot09.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        /*得到原始文件名*/
        String fileName = picFile.getOriginalFilename();
        System.out.println("文件名：" + fileName);

        String suffix = fileName.substring(fileName.lastIndexOf("."));

        fileName = UUID.randomUUID() + suffix;
        System.out.println("唯一文件名：" + fileName);

        String dirPath = "d:/files";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String filePath = dirPath + "/" + fileName;
        picFile.transferTo(new File(filePath));
        return fileName;
    }

    @RequestMapping("/remove")
    public void remove(String name) {
        // name=xxxx.jpg   d:/files/xxxx.jpg
        //拼接文件完整路径 删除文件
        new File("d:/files/"+name).delete();
    }


}
